var myApp = angular.module('myApp', [ 'ngGrid', 'ui.select2' ]);
myApp.controller('shipmentCtrl', function($scope, $http) {
	//global variable
	$scope.selectedRows = [];
	$scope.selectedRow = [];
	$scope.selectedRowL = [];
	$scope.vehicleTypeList;
	var shipmentObject = null;
	//init page item
	$scope.init = function() {
		$('input[name="time"],input[name="planltime"],input[name="planatime"]').datetimepicker({
			minView: "month",
			format: "yyyy-mm-dd hh:ii",
		    todayBtn: "linked",
		    language: "zh-CN",
		    forceParse: false,
		    autoclose: true,
		    todayHighlight: true
		});
		httpPost($http, {"url":ctx + "/client/findListByFirst", "ifBlock" : true}, function(data){
			$scope.clientList = data.dt;
		}, function(){
			toastError("初始化客户列表失败。");
		});
		httpPost($http, {"url":ctx + "/constractor/findListBy", "data": {"ffMap":{"st" : 0}}, "ifBlock" : true}, function(data){
			$scope.constractorList = data.dt;
		}, function(){
			toastError("初始化承运商列表失败。");
		});
		httpPost($http, {"url":ctx + "/driver/findAllBy", "ifBlock" : true}, function(data){
			$scope.driverList = data.dt;
		}, function(){
			toastError("初始化司机列表失败。");
		});	
		httpPost($http, {"url":ctx + "/transLocation/findListByFirst", "ifBlock" : true}, function(data){
			$scope.translocationList = data.dt;
		}, function(){
			toastError("初始化运输地列表失败。");
		});	
		httpPost($http, {"url":ctx + "/dict/getDictByCd", "params":{"cd" : "shipmentMethod"}, "ifBlock" : true}, function(data){
			$scope.shipment_methodList = data.dt;
		}, function(){
			toastError("初始化运输方式列表失败。");
		});	
		httpPost($http, {"url":ctx + "/dict/getDictByCd", "params":{"cd" : "vocation"}, "ifBlock" : true}, function(data){
			$scope.cdList = data.dt;	
		}, function(){
			toastError("初始化业务类型列表失败。");
		});
		httpPost($http, {"url":ctx + "/dict/getDictByCd", "params":{"cd" : "shipment"}, "ifBlock" : true}, function(data){
			$scope.shipmentList = data.dt;	
		}, function(){
			toastError("初始化状态列表失败。");
		});
		httpPost($http, {"url":ctx + "/vehicleType/findAllBy", "ifBlock" : true}, function(data){
			$scope.vehicleTypeList = data.dt;	
		}, function(){
			toastError("初始化状态列表失败。");
		});	
	};
	$scope.init();
	
	//grid
	$scope.pagingOptions = {
		pageSizes : [ 20, 50, 100 ],
		pageSize : 20,
		currentPage : 1,
		onQueryFlag : false
	};
	$scope.$watch('pagingOptions', function(newVal, oldVal) {
		if (newVal !== oldVal/* && newVal.currentPage !== oldVal.currentPage*/) {
			$scope.query();
		}
	}, true);
	$scope.gridOptions = {
		data : 'dt',
		totalServerItems : 'tt',
		columnDefs : [
		{
			field : "id",
			displayName : "ID", visible : false
		},
		{
			field : "vocationType",
			displayName : "vocationType", visible : false
		},
		{
			field : "cd",
			displayName : "调度单号",
			width : 140
		},
		{
			field : "ftranslocationName",
			displayName : "出发地",
			width : 120
		},
		{
			field : "ttranslocationName",
			displayName : "目的地",
			width : 120
		},
		{
			field : "points",
			displayName : "送货点数",
			width : 120
		},
		{
			field : "quantity",
			displayName : "件数",
			width : 120
		},
		{
			field : "weight",
			displayName : "重量",
			width : 120
		},
		{
			field : "volume",
			displayName : "体积",
			width : 120
		},
		{
			field : "palletsum",
			displayName : "托数",
			width : 120
		},
		{
			field : "constractorName",
			displayName : "承运商",
			width : 160
		},	
		{
			field : "carNo",
			displayName : "车牌号",
			width : 120
		},
		{
			field : "vehicleTypeName",
			displayName : "车型",
			width : 120
		},
		{
			field : "driverName",
			displayName : "司机",
			width : 120
		},
		{
			field : "statusName",
			displayName : "调度单状态",
			width : 120
		},
		{
			field : "vocationTypeName",
			displayName : "业务类型",
			width : 120
		},
		{
			field : "shipment_methodName",
			displayName : "运输方式",
			width : 120
		},
		{
			field : "expense",
			displayName : "一口价",
			width : 120
		},
		{
			field : "expectMoney",
			displayName : "预计费用",
			width : 120
		},
		{
			field : "planltime",
			displayName : "计划发车时间",
			width : 140,
			cellFilter : "date:'yyyy-MM-dd HH:mm'"
		},
		{
			field : "planatime",
			displayName : "预计到达时间",
			width : 140,
			cellFilter : "date:'yyyy-MM-dd HH:mm'"
		},
		{
			field : "leavetime",
			displayName : "出发时间",
			width : 140,
			cellFilter : "date:'yyyy-MM-dd HH:mm'"
		},
		{
			field : "arrivetime",
			displayName : "到达时间",
			width : 140,
			cellFilter : "date:'yyyy-MM-dd HH:mm'"
		},
		{
			field : "gzNo",
			displayName : "跟踪单号",
			width : 120
		},
		{
			field : "descr",
			displayName : "描述",
			width : 180
		}
		],
		enablePaging : true,
		pagingOptions : $scope.pagingOptions,
		enableCellEdit : true,
		showSelectionCheckbox : true,
		selectedItems : $scope.selectedRows,
		plugins : [ /*new ngGridFlexibleHeightPlugin()*/ ],
		i18n: 'zh-cn', rowHeight :21, footerRowHeight : 35,
		showFooter : true
	};

	$scope.query = function() {
		closeModal($("#queryFormModal"));
		var requestBody = {
			pageNb : $scope.pagingOptions.currentPage,
			pageSize : $scope.pagingOptions.pageSize,
			ffMap : $scope.queryForm
		};
		setTimeout(function() {
			httpPost($http, {"url":ctx + '/shipment/findListBy', "data" : requestBody, "ifBlock" : true}, function(data){
				$scope.tt = data.tt;
				$scope.dt = data.dt;
				$scope.gridOptions.$gridScope.toggleSelectAll(false);
				if(data.dt.length === 0){
					toastWarning("没有找到符合查询条件的记录。");
				};
			}, function(errMsg){
				toastError("查询失败。" + errMsg);
			});
		}, 100);
	};
	//trigger query when currentPage not change
	$scope.onQuery = function(){
		$scope.pagingOptions.onQueryFlag = !$scope.pagingOptions.onQueryFlag;
		$scope.pagingOptions.currentPage = 1;
	};

	$scope.refresh = function(){
		hideShadow();
		$scope.onQuery();	
	};

	//导出
	$scope.onOpenExportFormModal = function(){	
		var params;
		if($scope.queryForm == undefined){
			params = "";
		}else{
			params = $scope.queryForm;
		}
		var str = jQuery.param(params);
		window.open(ctx  + '/shipment/export?' + str);
	};

	//更改一口价
	$scope.onOpenExpensetFormModal = function(){
		$scope.editFormEx={};
		if($scope.selectedRows.length != 1) { toastWarning("请选择一笔记录进行编辑。"); return; }
		hideShadow();
		openModal($("#importFormModal"));
	};
	
	$scope.onChangeEx = function(){
		httpPost($http, {"url":ctx + '/shipment/updExpenseById',"params": {"id" : $scope.selectedRows[0]['id'], "expense" : $scope.editFormEx.expense}, "ifBlock" : true}, function(data){
			$scope.onQuery();
		});
		closeModal($("#importFormModal"));
	};
	
	//utils
	$scope.onClearSelectedRows = function() {
		$scope.gridOptions.$gridScope.toggleSelectAll(false);
	};
		
	//grid
	$scope.pagingOption = {
		pageSizes : [ 20, 50, 100 ],
		pageSize : 20,
		currentPage : 1,
		onQueryFlag : false
	};
	$scope.$watch('pagingOption', function(newVal, oldVal) {
		if (newVal !== oldVal/* && newVal.currentPage !== oldVal.currentPage*/) {
			$scope.queryl();
		}
	}, true);
	$scope.gridOption = {
		data : 'd',
		totalServerItems : 't',	
		columnDefs : [
		{
			field : "id",
			displayName : "ID", visible : false
		},
		{
			field : "orderId",
			displayName : "orderId", visible : false
		},
		{
			field : "tempshipmentId",
			displayName : "tempshipmentId", visible : false
		},
		{
			field : "vocationType",
			displayName : "业务类型",visible : false
		},
		{
			field : "orderCd",
			displayName : "订单号",
			width : 140
		},
		{
			field : "legs_no",
			displayName : "分段号",
			width : 120
		},
		{
			field : "shipment_methodName",
			displayName : "运输方式",
			width : 120
		},
		{
			field : "ftranslocationName",
			displayName : "出发地",
			width : 160
		},
		{
			field : "ttranslocationName",
			displayName : "目的地",
			width : 160
		},
		{
			field : "quantity",
			displayName : "件数",
			width : 120
		},
		{
			field : "weight",
			displayName : "重量",
			width : 120
		},
		{
			field : "volume",
			displayName : "体积",
			width : 120
		},
		{
			field : "palletsum",
			displayName : "托数",
			width : 120
		},
		{
			field : "formName",
			displayName : "发货方名称",
			width : 250
		},
		{
			field : "toName",
			displayName : "收货方名称",
			width : 250
		},
		{
			field : "toAddr",
			displayName : "收货方地址",
			width : 300
		},
		{
			field : "planltime",
			displayName : "计划离开时间",
			width : 160,
			cellFilter : "date:'yyyy-MM-dd HH:mm'"
		},
		{
			field : "descr",
			displayName : "描述",
			width : 180
		}
		],
		enablePaging : true,
		pagingOptions : $scope.pagingOption,
		enableCellEdit : true,
		showSelectionCheckbox : true,
		selectedItems : $scope.selectedRow,
		plugins : [ /*new ngGridFlexibleHeightPlugin()*/ ],
		i18n : 'zh-cn', rowHeight : 21, footerRowHeight : 35,
		showFooter : true,
		afterSelectionChange : function(rowItem, event){
			if($scope.selectedRow.length > 0){
				$scope.cancelflag = false;
			}else{
				$scope.cancelflag = true;
			}
		}
	};

	$scope.queryl = function() {
		closeModal($("#queryFormModal"));
		var requestBody = {
			pageNb : $scope.pagingOption.currentPage,
			pageSize : $scope.pagingOption.pageSize,
			ffMap : $scope.queryForml
		};
		setTimeout(function() {
			httpPost($http, {"url":ctx + '/legs/findListBy', "data" : requestBody, "ifBlock" : true}, function(data){
				$scope.t = data.tt;
				$scope.d = data.dt;
				$scope.gridOption.$gridScope.toggleSelectAll(false);
			}, function(errMsg){
				toastError("查询失败。" + errMsg);
			});
		}, 100);
	};
	//trigger query when currentPage not change
	$scope.onQueryl = function(){
		$scope.pagingOption.onQueryFlag = !$scope.pagingOption.onQueryFlag;
		$scope.pagingOption.currentPage = 1;
	};
	
	
	//grid
	$scope.pagingOptionL = {
		pageSizes : [ 20, 50, 100 ],
		pageSize : 20,
		currentPage : 1,
		onQueryFlag : false
	};
	$scope.$watch('pagingOptionL', function(newVal, oldVal) {
		if (newVal !== oldVal/* && newVal.currentPage !== oldVal.currentPage*/) {
			$scope.queryLg();
		}
	}, true);
	$scope.gridOptionL = {
		data : 'dl',
		totalServerItems : 'tl',
		
		columnDefs : [
		{
			field : "id",
			displayName : "ID", visible : false
		},
		{
			field : "orderCd",
			displayName : "订单号",
			width : 140
		},
		{
			field : "legs_no",
			displayName : "调度单号",
			width : 140
		},
		{
			field : "vocationName",
			displayName : "业务类型",
			width : 120
		},
		{
			field : "billtime",
			displayName : "出发时间",
			width : 140
		},
		{
			field : "clientName",
			displayName : "客户",
			width : 120
		},
		{
			field : "quantity",
			displayName : "总件数",
			width : 120
		},
		{
			field : "weight",
			displayName : "重量",
			width : 130
		},
		{
			field : "volume",
			displayName : "体积",
			width : 120
		},
		{
			field : "ftranslocationName",
			displayName : "出发地",
			width : 120
		},
		{
			field : "fromAddr",
			displayName : "出发地地址",width:120
		},
		{
			field : "ttranslocationName",
			displayName : "目的地",
			width : 120
		},
		{
			field : "toAddr",
			displayName : "目的地地址",
			width : 120
		},
		{
			field : "planltime",
			displayName : "计划发车时间",
			width : 140
		},
		{
			field : "planatime",
			displayName : "预计到达时间",
			width : 140
		}
		],
		enablePaging : true,
		pagingOptions : $scope.pagingOptionL,
		enableCellEdit : true,
		showSelectionCheckbox : true,
		selectedItems : $scope.selectedRowL,
		plugins : [ /*new ngGridFlexibleHeightPlugin()*/ ],
		i18n: 'zh-cn', rowHeight : 21, footerRowHeight : 35,
		showFooter : true
	};

	$scope.queryLg = function() {
		closeModal($("#queryFormModal"));
		var requestBody = {
			pageNb : $scope.pagingOptionL.currentPage,
			pageSize : $scope.pagingOptionL.pageSize,
			ffMap : $scope.queryFormlg
		};
		setTimeout(function() {
			httpPost($http, {"url":ctx + '/legs/findListBy', "data" : requestBody, "ifBlock" : true}, function(data){
				$scope.tl = data.tt;
				$scope.dl = data.dt;
				$scope.gridOptionL.$gridScope.toggleSelectAll(false);
			}, function(errMsg){
				toastError("查询失败。" + errMsg);
			});
		}, 100);
	};
	//trigger query when currentPage not change
	$scope.onQueryLg = function(){
		$scope.pagingOptionL.onQueryFlag = !$scope.pagingOptionL.onQueryFlag;
		$scope.pagingOptionL.currentPage = 1;
	};
	
	//派车，车辆入库，车辆出库，发车，运抵确认。
	$scope.onOpenConfirmFormModal = function(type) {
		$scope.editForm = {};
		if(type == 'pc'){
			$('h4').text('派车确认');
			$scope.editForm.status = 'sendCar';
		}else if(type == 'rk'){
			$('h4').text('车辆入库确认');
			$scope.editForm.status = 'instorage';
		}else if(type == 'ck'){
			$('h4').text('车辆出库确认');
			$scope.editForm.status = 'outstorage';
		}else if(type == 'fy'){
			$('h4').text('发运确认');
			$scope.editForm.status = 'onway';
		}else if(type == 'yd'){
			$('h4').text('运抵确认');
			$scope.editForm.status = 'arrive';
		}
		var date= (new Date());
		var now = date.getFullYear() + "-" + (parseInt(date.getMonth()) + 1) + "-" + ((date.getDate() > 9) ? date.getDate() : "0" + date.getDate()) + " " + ((date.getHours()> 9) ? date.getHours() : "0" + date.getHours()) + ":" + ((date.getMinutes() > 9) ? date.getMinutes() : "0" + date.getMinutes());
		$scope.editForm.time = now;
		hideShadow();
		openModal($("#confirmFormModal"));
	};
	
	$scope.Confirm = function(){
		closeModal($("#confirmFormModal"));
		var selectedRowIds = [];
		var selectedShipmentIds = [];
		$.each($scope.selectedRows, function(idx, obj){
			selectedRowIds.push(obj.id);
			var shipmentId=$scope.selectedRows[idx]['shipmentId'];
			if(jQuery.inArray( shipmentId, selectedShipmentIds ) < 0){
				selectedShipmentIds.push(shipmentId);
			}
		});
		
		httpPost($http, {"url":ctx + '/shipment/updStatusByIds', "params": {"shipmentIds":selectedRowIds, "status" : $scope.editForm.status, "time" : $scope.editForm.time}, "ifBlock":true}, function(data){
			$scope.onQuery();
		});
	};
	
	//取消分段订单
	$scope.cancel=function(){
		var selectedRowId = [];
		var orderId = [];
		var object = [];
		$.each($scope.selectedRow, function(idx, obj){
			selectedRowId.push(obj.id);
			orderId.push($scope.selectedRow[idx]['orderId']);
			object.push($scope.selectedRow[idx]);
		});
		httpPost($http, {"url":ctx + '/legs/updByIds',"params":{"shipmentId" : $scope.selectedRows[0]['id'], "ids" : selectedRowId, "orderId" : orderId, "vocationType" : $scope.selectedRows[0]['vocationType']}, "ifBlock" : true}, function(data){
			toastSuccess("取消成功");
			ids = {"shipmentId" : $scope.selectedRows[0]['id']};
			$scope.onQueryl = function(){
				$scope.pagingOption.onQueryFlag = !$scope.pagingOption.onQueryFlag;
				$scope.pagingOption.currentPage = 1;
				$scope.queryForml = ids;
			};	
			$scope.onQueryl();
			httpGet($http, {"url":ctx + '/shipment/id/' + $scope.selectedRows[0]['id'], "ifBlock" : true}, function(data){
				$scope.editForm = data.dt;
				if($scope.editForm.planatime != null){
					$scope.editForm.planatime = date2Ymdhms(data.dt.planatime);
				}
				if($scope.editForm.planltime != null){
					$scope.editForm.planltime = date2Ymdhms(data.dt.planltime);
				}
					$scope.selectedRows[0] = data.dt;
			});
		}, function(errMsg){
			toastError("取消失败。" + errMsg);
		});	
	};
	//utils
	$scope.onClearSelectedRows = function() {
		$scope.gridOption.$gridScope.toggleSelectAll(false);
	};
	
	//crud
	$scope.onOpenQueryFormModal = function() {	
		hideShadow();
		openModal($("#queryFormModal"));
	};
	
	$scope.onFilter = function(){
		$scope.onQueryLg();
	};
	
	//添加分段订单
	$scope.onOpenEditFormModalL = function(){
		var vocationType = shipmentObject['vocationType'];
		httpPost($http, {"url":ctx + '/tempShipment/findListBy',"data": {"ffMap":{"code" : "未配载", "vocation" : vocationType}}, "ifBlock" : true}, function(data){
			var id = data.dt[0]['id'];
			ids = {"tempshipmentId" : id};
			$scope.onQueryLg = function(){
				$scope.pagingOptionL.onQueryFlag = !$scope.pagingOptionL.onQueryFlag;
				$scope.pagingOptionL.currentPage = 1;
				$scope.queryFormlg = ids;
			};	
			$scope.onQueryLg();
		});
		openModal($("#editFormModalL"));
	};
	
	$scope.add = function(){
		closeModal($("#editFormModalL"));
		var selectedRowLId = [];
		var object = [];
		$.each($scope.selectedRowL, function(idx, obj){
			selectedRowLId.push(obj.id);
			object.push($scope.selectedRowL[idx]);
		});
		httpPost($http, {"url":ctx + '/legs/updByIds1', "params":{"ids" : selectedRowLId, "shipmentId" : shipmentObject['id'], "constractorId" : shipmentObject['constractorId'], "shipment_method" : shipmentObject['shipment_method'], "vocationType" : shipmentObject['vocationType']}, "ifBlock" : true}, function(data){
			ids={"shipmentId":shipmentObject['id']};
			$scope.onQueryl = function(){
				$scope.pagingOption.onQueryFlag = !$scope.pagingOption.onQueryFlag;
				$scope.pagingOption.currentPage = 1;
				$scope.queryForml = ids;
			};	
			$scope.onQueryl();
			httpGet($http, {"url":ctx + '/shipment/id/' + shipmentObject['id'], "ifBlock" : true}, function(data){
				$scope.editForm = data.dt;
				if($scope.editForm.planatime != null){
					$scope.editForm.planatime = date2Ymdhms(data.dt.planatime);
				}
				if($scope.editForm.planltime != null){
					$scope.editForm.planltime = date2Ymdhms(data.dt.planltime);
				}
				$scope.selectedRows[0] = data.dt;
			});
		});
	};
	$scope.onOpenEditFormModal = function(type, id) {
		$scope.co = true;
		$scope.vt = true;
		if(type == "add"){
			$("#cd").css("display","none");
			$("#ft").css("margin-left","25px");
			$("#tt").css("margin-left","-30px");
			$scope.addflag = true;
			$scope.cancelflag = true;
			$scope.flagc = false;
			$scope.editForm = {};
			$scope.editForm.shipment_method = 'TRUCK';
			$scope.editForm.vocationType = 'NORMAL';
			$scope.editForm.quantity = "0.0";
			$scope.editForm.weight = "0.0";
			$scope.editForm.volume = "0.0";
			$scope.editForm.points = "0";
			$scope.editForm.expense = "0.0";
			$scope.editForm.palletsum = "0.0";
			$scope.editForm.expectMoney = "0.0";
			$("#c").change(function(){
				var id = $scope.editForm.constractorId;
				httpGet($http, {"url":ctx + '/constractor/id/' + id, "ifBlock" : true}, function(data){	
					httpGet($http, {"url":ctx + '/vehicle/constractorId/' + id, "ifBlock" : true}, function(data){
						$scope.vehicleList = data.dt;				
					}, function(){
						toastError("初始化运输工具列表失败。");
					});
				});	
			});
			$("#vh").click(function(){
				var vid = $scope.editForm.vehicleId;
				httpGet($http, {"url":ctx + '/vehicle/id/' + vid, "ifBlock" : true}, function(data){
					$scope.editForm.carNo = data.dt['vehicleNo'];
						$scope.editForm.carNo = data.dt['vehicleNo'];
						$scope.editForm.vehicleTypeId = data.dt['vehicleTypeId'];
						$scope.editForm.driverId = data.dt['driverId'];
						httpGet($http, {"url":ctx + '/driver/id/' + $scope.editForm.driverId, "ifBlock" : true}, function(data){
							$scope.editForm.driverName = data.dt['name'];
							$scope.editForm.idcard = data.dt['idcard'];
						$scope.editForm.phone = data.dt['phone'];
						}, function(){
							toastError("初始化运输工具列表失败。");
						});
					}, function(){
					toastError("初始化运输工具列表失败。");
				});
			});
			$("#d").change(function(){
				var id = $scope.editForm.driverId;
				httpGet($http, {"url":ctx + '/driver/id/' + id, "ifBlock" : true}, function(data){
					$scope.editForm.driverName = data.dt['name'];
					$scope.editForm.idcard = data.dt['idcard'];
					$scope.editForm.phone = data.dt['phone'];
				}, function(){
					toastError("初始化运输工具列表失败。");
				});
			});
			ids={"shipmentId":0};
			$scope.onQueryl = function(){
				$scope.pagingOption.onQueryFlag = !$scope.pagingOption.onQueryFlag;
				$scope.pagingOption.currentPage = 1;
				$scope.queryForml = ids;
			};	
			$scope.onQueryl();
		}else if(type == "upd"){
			$scope.addflag = false;
			$scope.cancelflag = true;
			$scope.flagc = true;
			$("#cd").css("display","block");
			$("#ft").css("margin-left","0px");
			$("#tt").css("margin-left","0px");
			$scope.flagOfCd = true;
			if($scope.selectedRows.length != 1) { toastWarning("请选择一笔记录进行编辑。"); return; }
			shipmentObject=$scope.selectedRows[0];
			httpGet($http, {"url":ctx + '/shipment/id/' + $scope.selectedRows[0]['id'], "ifBlock" : true}, function(data){
				$scope.editForm = data.dt;
				if($scope.editForm.planatime != null){
					$scope.editForm.planatime = date2Ymdhms(data.dt.planatime);
				}
				if($scope.editForm.planltime != null){
					$scope.editForm.planltime = date2Ymdhms(data.dt.planltime);
				}
				var id = $scope.editForm['constractorId'];
				httpGet($http, {"url":ctx + '/vehicle/constractorId/' + id, "ifBlock" : true}, function(data){
					$scope.vehicleList = data.dt;				
				}, function(){
					toastError("初始化运输工具列表失败。");
				});
				$("#vh").click(function(){
					var vid = $scope.editForm.vehicleId;
					httpGet($http, {"url":ctx + '/vehicle/id/' + vid, "ifBlock" : true}, function(data){
						$scope.editForm.carNo = data.dt['vehicleNo'];
					}, function(){
						toastError("初始化运输工具列表失败。");
					});
				});
				
			}, function(errMsg){
				toastWarning("查询此笔记录失败。");
			});
			var id = $scope.selectedRows[0]['id'];
			$scope.queryForml = {};
			ids = {"shipmentId" : id};
			$scope.onQueryl = function(){
				$scope.pagingOption.onQueryFlag = !$scope.pagingOption.onQueryFlag;
				$scope.pagingOption.currentPage = 1;
				$scope.queryForml = ids;
			};	
			$scope.onQueryl();
			$(".close").click(function(){
				$scope.query();
			});
			$("#btn").click(function(){
				$scope.query();
			});
		}
		openModal($("#editFormModal"));
		$(".modal-body").css("padding","5px").css("overflow","visible");
		$(".modal-body").css("overflow","hidden");
		$("#mainDatas .ngViewport").height(180);
	};
	$scope.onSaveEditForm = function(){
		closeModal($("#editFormModal"));
		if($scope.editForm.planatime != undefined){
			$scope.editForm.planatime = ymdhms2date($scope.editForm.planatime);
		}else{
			$scope.editForm.planatime = null;
		}
		if($scope.editForm.planltime != undefined){
			$scope.editForm.planltime = ymdhms2date($scope.editForm.planltime);		
		}else{
			$scope.editForm.planltime = null;
		}
		httpPost($http, {"url":ctx + '/shipment/save', "data" : $scope.editForm, "ifBlock" : true}, function(data){
			toastSuccess("保存成功");
			$scope.query();
		}, function(errMsg){
			toastError("保存失败,您输入的信息有误，请检查正确之后再保存。" + errMsg);
		});
	};
	$scope.onOpenDelFormModal = function() {
		var selectedRowsLength = $scope.selectedRows.length;
		if(selectedRowsLength < 1) { toastWarning("请至少选择一笔记录进行删除。"); return; }
		dialogConfirm("是否确认删除选择的" + selectedRowsLength + "笔记录？", function(){
			var selectedRowsId = [];
			$.each($scope.selectedRows, function(idx, obj){
				selectedRowsId.push(obj.id);
			});
			
			httpPost($http, {"url":ctx + '/shipment/updStByIds', "params":{"ids" : selectedRowsId, "st" : 1}, "ifBlock" : true}, function(data){
				toastSuccess("删除成功");
				$scope.query();
			}, function(errMsg){
				toastError("删除失败。" + errMsg);
			});
		});
	};
	//右键菜单功能
	$('#mainDataBlock').contextMenu('menu', {
	      //菜单项样式
	      itemStyle: {
	        fontFamily : 'verdana',
	        backgroundColor : '#F0F0F0',
	        border: 'none',
	        padding: '4px'
	      },
	      //菜单项鼠标放在上面样式
	      itemHoverStyle: {
	        border: '1px solid #fff'
	      },
	      onShowMenu: function(e, menu) {
	    	var sts = [];
			for(var i = 0; i < $scope.selectedRows.length; i++){
				var st = $scope.selectedRows[i]['status'];
				if(jQuery.inArray( st, sts ) < 0){
					sts.push(st);
				}
			}	
	    	  
	    	if($scope.selectedRows.length === 0){
	          $('#edit, #del, #upd, #confirm, #in, #out, #send, #arrive, #specify, #refresh1, #excrete', menu).remove();
	        }else if($scope.selectedRows.length>1){
	        	if(sts.length === 1 && sts[0] === 'shipmentEd'){
	        		$('#edit', menu).remove();
	        	}else if(sts.length === 1 && sts[0] === 'sendCar'){
	        		$('#confirm,#edit', menu).remove();
	        	}else if(sts.length === 1 && sts[0] === 'instorage'){
	        		$('#edit,#confirm,#in', menu).remove();
	        	}else if(sts.length === 1 && sts[0] === 'outstorage'){
	        		$('#edit,#confirm,#in,#out', menu).remove();
	        	}else if(sts.length === 1 && sts[0] === 'onway'){
	        		$('#edit,#confirm,#in,#out,#send', menu).remove();
	        	}else if(sts.length === 1 && sts[0] === 'arrive'){
	        		$('#edit,#confirm,#in,#out,#send,#arrive', menu).remove();
	        	}else{
	  	          $('#edit,#confirm,#in,#out,#send,#arrive', menu).remove();
	        	}
	        }else{
		        	if(sts[0] === 'sendCar'){
		        		$('#confirm', menu).remove();
		        	}else if(sts[0] === 'instorage'){
		        		$('#confirm,#in', menu).remove();
		        	}else if(sts[0] === 'outstorage'){
		        		$('#confirm,#in,#out', menu).remove();
		        	}else if(sts[0] === 'onway'){
		        		$('#confirm,#in,#out,#send', menu).remove();
		        	}else if(sts[0] === 'arrive'){
		        		$('#confirm,#in,#out,#send,#arrive', menu).remove();
		        	}
	        }
	        return menu;
	      }
		});
});